package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {
   @Autowired
    private OrderSettingService orderSettingService;
   //下载模版文件
    @GetMapping("/download")
    public void downloadTemplate(@RequestParam String filename, HttpServletResponse response) {
        try {
        // 使用 ClassPathResource 读取 resources 下的文件
            ClassPathResource resource = new ClassPathResource("templates/" + filename);

            if (!resource.exists()) {
                response.setStatus(HttpServletResponse.SC_NOT_FOUND);
                response.getWriter().write("文件不存在");
                return;
            }

             // 设置响应头（下载 Excel）
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-Disposition", "attachment;filename="
                    + URLEncoder.encode(filename, "UTF-8"));

            // 写入文件流
            InputStream in = resource.getInputStream();
            OutputStream out = response.getOutputStream();

            byte[] buffer = new byte[1024];
            int len;
            while ((len = in.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }

            in.close();
            out.flush();
            out.close();
        } catch (IOException e) {
            throw new RuntimeException("文件下载失败", e);
        }
    }
//  根据日期编辑预约设置
    @PostMapping("/editNumberByOrderDate")
    public Result editNumberByOrderDate(@RequestBody OrderSetting orderSetting) {
        try {
            return orderSettingService.editNumberByOrderDate(orderSetting);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }
   //查询对应年月下的预约设置
    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String month){
        try {
            return orderSettingService.getOrderSettingByMonth(month);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }
//  批量导入预约设置
    @PostMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {
        try {
            return orderSettingService.upload(excelFile);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }
}
